home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / des_c.exe / lha / GETPASS.C < prev    next >
Text File  |  1990-07-14  |  1KB  |  82 lines

  1. #include <stdio.h>
  2. #ifdef __TURBOC__
  3. #include <conio.h>
  4. #else
  5. #include <signal.h>
  6. #include <sgtty.h>
  7.  
  8. #define    TTY    "/dev/tty"
  9. #endif
  10.  
  11. /* Issue prompt and read reply with echo turned off */
  12. char *
  13. getpass(prompt)
  14. #ifdef __TURBOC__
  15. const char *prompt;
  16. #else
  17. char *prompt;
  18. #endif
  19. {
  20. #ifndef __TURBOC__
  21.     struct sgttyb ttyb,ttysav;
  22.     FILE *tty;
  23.     int (*signal())(),(*sig)();
  24. #endif
  25.     register char *cp;
  26.     int c;
  27.     static char pbuf[128];
  28.  
  29. #ifndef __TURBOC__
  30.     if ((tty = fdopen(open(TTY, 2), "r")) == NULL)
  31.         tty = stdin;
  32.     else
  33.         setbuf(tty, (char *)NULL);
  34.     sig = signal(SIGINT, SIG_IGN);
  35.     ioctl(fileno(tty), TIOCGETP, &ttyb);
  36.     ioctl(fileno(tty), TIOCGETP, &ttysav);
  37.     ttyb.sg_flags |= RAW;
  38.     ttyb.sg_flags &= ~ECHO;
  39.     ioctl(fileno(tty), TIOCSETP, &ttyb);
  40. #endif
  41.  
  42.     fprintf(stderr, "%s", prompt);
  43.     fflush(stderr);
  44.     cp = pbuf;
  45.     for (;;) {
  46. #ifdef __TURBOC__
  47. loop:        c = getch();
  48.         if (!c)  {
  49.             c = getch();
  50.             goto loop;
  51.         }
  52.  
  53.         if (c == '\r' || c == '\n' || c == EOF)
  54.             break;
  55.  
  56.         if (cp < &pbuf[127])
  57.             *cp++ = c;
  58. #else
  59.         c = getc(tty);
  60.         if(c == '\r' || c == '\n' || c == EOF)
  61.             break;
  62.         if (cp < &pbuf[127])
  63.             *cp++ = c;
  64. #endif
  65.     }
  66.     *cp = '\0';
  67. #ifdef __TURBOC__
  68.     fprintf(stderr,"\n");
  69. #else
  70.     fprintf(stderr,"\r\n");
  71. #endif
  72.     fflush(stderr);
  73.  
  74. #ifndef __TURBOC__
  75.     ioctl(fileno(tty), TIOCSETP, &ttysav);
  76.     signal(SIGINT, sig);
  77.     if (tty != stdin)
  78.         fclose(tty);
  79. #endif
  80.     return(pbuf);
  81. }
  82.